package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.SysMsg;
import com.xjc.business.entity.domain.WxUser;
import com.xjc.business.entity.po.ChatMsgPo;
import com.xjc.business.entity.po.MsgPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.MsgVo;
import com.xjc.business.entity.vo.classadmin.ChatMsgContainer;
import com.xjc.business.service.SchoolsService;
import com.xjc.business.service.StudentsService;
import com.xjc.business.service.SysMsgService;
import com.xjc.business.mapper.SysMsgMapper;
import com.xjc.business.service.WxUserService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【sys_msg(系统消息表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:33
*/
@Service
public class SysMsgServiceImpl extends ServiceImpl<SysMsgMapper, SysMsg>
    implements SysMsgService {

    @Autowired
    @Lazy
    SchoolsService schoolsService;


    /**
     * 获取消息分页
     *
     * @param pageSize    每页条数
     * @param currentPage 当前页码
     * @param readStatus  阅读状态
     * @param type        类型
     * @return 分页数据
     */
    @Override
    public Result<PageVo<MsgVo>> getMsgPage(Integer sendId,Integer pageSize, Integer currentPage, Integer readStatus, Integer type) {
        LambdaQueryWrapper<SysMsg> sysMsgLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysMsgLambdaQueryWrapper.eq(readStatus!=-1,SysMsg::getReadStatus,readStatus);
        sysMsgLambdaQueryWrapper.eq(type!=-1,SysMsg::getType,type);
        sysMsgLambdaQueryWrapper.eq(SysMsg::getSendId,sendId);

        Page<SysMsg> sysMsgPage = new Page<>(currentPage, pageSize);

        page(sysMsgPage,sysMsgLambdaQueryWrapper);

        List<SysMsg> sysMsgPageRecords = sysMsgPage.getRecords();
        List<MsgVo> msgVoList = sysMsgPageRecords.stream().map(sysMsg -> {
            MsgVo msgVo = new MsgVo();
            msgVo.setId(sysMsg.getId());
            msgVo.setReviceName(schoolsService.getById(sysMsg.getReviceId()).getSchoolName());
            msgVo.setReadStatus(MyConstants.MSG_READ_STATUS.get(sysMsg.getReadStatus()));
            msgVo.setMsg(sysMsg.getMsg());
            msgVo.setPic(sysMsg.getPic());
            msgVo.setFile(sysMsg.getFile());
            msgVo.setVideo(sysMsg.getVideo());
            msgVo.setIcon(sysMsg.getIcon());
            msgVo.setType(MyConstants.MSG_TYPE.get(sysMsg.getType()));
            msgVo.setCreateTime(sysMsg.getCreateTime());
            return msgVo;
        }).collect(Collectors.toList());

        PageVo<MsgVo> pageVo = new PageVo<>();
        pageVo.setData(msgVoList);
        pageVo.setTotal(sysMsgPage.getTotal());
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);
        pageVo.setPageCount(sysMsgPage.getPages());

        return Result.okWithData(pageVo);
    }

    /**
     * 新增消息
     * @param msgPo 消息
     * @return 反馈
     */
    @Override
    public Result<String> addMsg(MsgPo msgPo) {
        SysMsg sysMsg = new SysMsg();
        sysMsg.setSendId(msgPo.getSendId());
        sysMsg.setReviceId(msgPo.getReviceId());
        sysMsg.setReadStatus(MyConstants.MSG_NOT_READ);
        sysMsg.setMsg(msgPo.getMsg());
        sysMsg.setPic(msgPo.getPic());
        sysMsg.setFile(msgPo.getFile());
        sysMsg.setVideo(msgPo.getVideo());
        sysMsg.setIcon(msgPo.getIcon());
        sysMsg.setType(msgPo.getType());
        sysMsg.setCreateTime(new Date());
        sysMsg.setUpdateTime(new Date());
        save(sysMsg);
        return Result.ok("新增成功");
    }

    /**
     * 获取消息分页
     *
     * @param schoolId      接收方id
     * @param pageSize    每页条数
     * @param currentPage 当前页码
     * @param readStatus  阅读状态
     * @param type        类型
     * @return 分页数据
     */
    @Override
    public Result<PageVo<MsgVo>> getMsgPageWithUserId(Integer schoolId, Integer pageSize, Integer currentPage, Integer readStatus, Integer type) {
        LambdaQueryWrapper<SysMsg> sysMsgLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysMsgLambdaQueryWrapper.eq(readStatus!=-1,SysMsg::getReadStatus,readStatus);
        sysMsgLambdaQueryWrapper.eq(type!=-1,SysMsg::getType,type);
        sysMsgLambdaQueryWrapper.eq(SysMsg::getReviceId,schoolId);

        Page<SysMsg> sysMsgPage = new Page<>(currentPage, pageSize);

        page(sysMsgPage,sysMsgLambdaQueryWrapper);

        List<SysMsg> sysMsgPageRecords = sysMsgPage.getRecords();
        List<MsgVo> msgVoList = sysMsgPageRecords.stream().map(sysMsg -> {
            MsgVo msgVo = new MsgVo();
            msgVo.setId(sysMsg.getId());
            msgVo.setFromName("系统");
            msgVo.setReadStatus(MyConstants.MSG_READ_STATUS.get(sysMsg.getReadStatus()));
            msgVo.setMsg(sysMsg.getMsg());
            msgVo.setPic(sysMsg.getPic());
            msgVo.setFile(sysMsg.getFile());
            msgVo.setVideo(sysMsg.getVideo());
            msgVo.setIcon(sysMsg.getIcon());
            msgVo.setType(MyConstants.MSG_TYPE.get(sysMsg.getType()));
            msgVo.setCreateTime(sysMsg.getCreateTime());
            return msgVo;
        }).collect(Collectors.toList());

        PageVo<MsgVo> pageVo = new PageVo<>();
        pageVo.setData(msgVoList);
        pageVo.setTotal(sysMsgPage.getTotal());
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);
        pageVo.setPageCount(sysMsgPage.getPages());

        return Result.okWithData(pageVo);
    }

    /**
     * 消息已读
     * @param id 消息id
     * @return 反馈
     */
    @Override
    public Result<String> readMsg(Integer id) {

        SysMsg sysMsg = getById(id);
        if (sysMsg!=null){
            sysMsg.setReadStatus(MyConstants.MSG_HAS_READ);
            updateById(sysMsg);
            return Result.okWithData("已读");
        }
        return Result.okWithData("已读失败");
    }



}




